package com.parking.carsystem.parkingchargesystem.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.parking.carsystem.parkingchargesystem.R;
import com.parking.carsystem.parkingchargesystem.common.Constant;
import com.parking.carsystem.parkingchargesystem.module.InvoiceMessageTitle;
import com.parking.carsystem.parkingchargesystem.module.InvoiceRecordModel;

import java.util.ArrayList;

import butterknife.BindView;
import ttyy.com.recyclerexts.base.EXTRecyclerAdapter;
import ttyy.com.recyclerexts.base.EXTViewHolder;
import ttyy.com.recyclerexts.base.MultiType;

public class InvoiceRecordActivity extends BaseActivity {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.invoice_record)
    RecyclerView invoiceRecord;
    private EXTRecyclerAdapter<InvoiceRecordModel.RowsBean> invoiceAdapter;
    ArrayList<InvoiceRecordModel.RowsBean> invoiceRecords;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_invoice_record;
    }

    @Override
    public void initView() {
        back.setVisibility(View.VISIBLE);
        text.setText("开票记录");
    }

    @Override
    public void initData() {
        if (invoiceRecords == null) {
            invoiceRecords = new ArrayList<>();
        }
        invoiceRecord.setLayoutManager(new LinearLayoutManager(this));
        MultiType<InvoiceRecordModel.RowsBean> multiType = new MultiType<InvoiceRecordModel.RowsBean>() {
            @Override
            public int getItemType(int i, InvoiceRecordModel.RowsBean rowsBean) {
                if (rowsBean.invoiceTitle.type.equals("01") || rowsBean.invoiceTitle.type.equals("03")) {
                    return 1;
                }
                return 0;
            }
        };
        multiType.add(0, R.layout.item_list_invoice_type);
        multiType.add(1, R.layout.item_invoice_record);
        invoiceAdapter = new EXTRecyclerAdapter<InvoiceRecordModel.RowsBean>(multiType) {
            @Override
            public void onBindViewHolder(EXTViewHolder extViewHolder, int i, final InvoiceRecordModel.RowsBean rowsBean) {
                int itemViewType = getItemViewType(i);
                if (itemViewType == 1) {
                    TextView detail_look = extViewHolder.findViewById(R.id.detail_look);
                    extViewHolder.setText(R.id.invoice_title, rowsBean.invoiceTitle.name);
                    extViewHolder.setText(R.id.invoice_time, rowsBean.createTime);
                    extViewHolder.setText(R.id.invoice_number, rowsBean.invoiceNo);
                    extViewHolder.setText(R.id.invoice_address_msg, rowsBean.pdfUrl);
                    detail_look.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //查看发票详情
                            Intent intent = new Intent(InvoiceRecordActivity.this, TbsReaderViewActivity.class);
                            intent.putExtra("pdfUrl",rowsBean.pdfUrl);
                            startActivity(intent);
                        }
                    });
                } else {
                    extViewHolder.setText(R.id.company_title, rowsBean.invoiceTitle.name);
                }
            }
        };
        invoiceAdapter.setDatas(invoiceRecords);
        invoiceRecord.setAdapter(invoiceAdapter);
        showLoading();
        OkGo.<String>get(Constant.url + Constant.INVOICELIST)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        stopLoading();
                        Gson gson = new Gson();
                        InvoiceRecordModel invoiceRecordModel = gson.fromJson(response.body(), InvoiceRecordModel.class);
                        ArrayList<InvoiceRecordModel.RowsBean> companyDatas;
                        companyDatas = new ArrayList<>();
                        ArrayList<InvoiceRecordModel.RowsBean> userDatas = new ArrayList<>();
                        if (invoiceRecordModel.code == 200) {
                            for (InvoiceRecordModel.RowsBean row : invoiceRecordModel.rows) {
                                if (row.invoiceTitle != null) {
                                    if (row.invoiceTitle.type.equals("01")) {
                                        companyDatas.add(row);
                                    } else if (row.invoiceTitle.type.equals("03")) {
                                        userDatas.add(row);
                                    }
                                }
                            }
                        }
                        if (companyDatas.size() > 0) {
                            InvoiceRecordModel.RowsBean rowsBean = new InvoiceRecordModel.RowsBean();
                            InvoiceRecordModel.RowsBean.InvoiceTitleBean invoiceTitleBean = new InvoiceRecordModel.RowsBean.InvoiceTitleBean();
                            invoiceTitleBean.name = "单位";
                            invoiceTitleBean.type = "10";
                            rowsBean.invoiceTitle = invoiceTitleBean;
                            invoiceRecords.add(rowsBean);
                            invoiceRecords.addAll(companyDatas);
                        }
                        if (userDatas.size() > 0) {
                            InvoiceRecordModel.RowsBean rowsBean1 = new InvoiceRecordModel.RowsBean();
                            InvoiceRecordModel.RowsBean.InvoiceTitleBean invoiceTitleBean = new InvoiceRecordModel.RowsBean.InvoiceTitleBean();
                            invoiceTitleBean.name = "个人";
                            invoiceTitleBean.type = "10";
                            rowsBean1.invoiceTitle = invoiceTitleBean;
                            invoiceRecords.add(rowsBean1);
                            invoiceRecords.addAll(userDatas);
                        }
                        invoiceAdapter.setDatas(invoiceRecords);
                        invoiceAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onError(Response<String> response) {
                        stopLoading();
                    }
                });
    }
}
